package bTree.LeetCode;

import java.util.Arrays;

public class 删掉一个元素以后全为1的最长子数组 {
    public int longestSubarray(int[] nums) {
        int length = nums.length;
        int result=0;
        if(length==1){
            return 0;
        }
        int left=0,right=1;
        int zeroIndex=nums[0]==0?0:-1;
        while (right<length){
            int num = nums[right];
            if (num==1){
                result=Math.max(result,right-left+1);
                right++;
            }else {
                if (zeroIndex==-1){
                    result=Math.max(result,right-left+1);
                    zeroIndex=right;
                    right++;
                }else {
                    left=zeroIndex+1;
                    zeroIndex=right;
                    right++;
                }
            }
        }
        return result==0?0:result-1;
    }
}